﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Brunch.Forms
{
	public partial class ConfigForm : BaseEditForm
	{
		BrunchConfig cfg;
		ProjectConfig selectedConfig;

		public ConfigForm(BrunchConfig config)
		{
			InitializeComponent();
			cfg = config;
			PopulateList();

			string className = this.GetType().Name;
		}

		private void PopulateList()
		{
			lvProjects.Items.Clear();
			if (cfg == null) return;
			foreach (var prjCfg in cfg.ProjectConfigs)
			{
				var lvItem = new ListViewItem();
				SetItemProperties(lvItem, prjCfg);
				lvProjects.Items.Add(lvItem);
			}
		}

		private void SetItemProperties(ListViewItem item, ProjectConfig cfg)
		{
			item.Text = cfg.ProjectName;
			if (item.SubItems.Count == 1)
				item.SubItems.Add(cfg.RootPath);
			else
				item.SubItems[1].Text = cfg.RootPath;
			item.Tag = cfg;
		}

		private void lvProjects_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (lvProjects.SelectedItems.Count == 0) return;
			selectedConfig = lvProjects.SelectedItems[0].Tag as ProjectConfig;
			selectedConfig.Tag = lvProjects.SelectedItems[0];
			btnEdit.Enabled = (selectedConfig != null);
			btnDelete.Enabled = (selectedConfig != null);
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			using(var fbd = new FolderBrowserDialog())
			{
				Common.ConfigureBrowseFolderDialog(fbd);

				if (fbd.ShowDialog() == DialogResult.OK)
				{
					string path = fbd.SelectedPath;
					using (var frm = new ProjectConfigForm())
					{
						var config = new ProjectConfig();
						config.RootPath = path;
						string[] chunks = path.Split(Path.DirectorySeparatorChar);
						config.ProjectName = chunks[chunks.Length - 1];

						frm.ProjectConfig = config;
						if (frm.ShowDialog() == DialogResult.OK)
						{
							cfg.ProjectConfigs.Add(config);
							PopulateList();
						}
					}
				}
			}
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			using (var frm = new ProjectConfigForm())
			{
				if (selectedConfig != null)
				{
					//pass the form a clone of the selected config
					//if user presses OK, copy the configured object's properties, otherwise ignore
					var clone = (ProjectConfig)selectedConfig.Clone();
					frm.ProjectConfig = clone;
					if (frm.ShowDialog() == DialogResult.OK)
					{
						selectedConfig.CopyPropertiesFrom(frm.ProjectConfig);
						ListViewItem lvItem = selectedConfig.Tag as ListViewItem;
						if (lvItem != null)
							SetItemProperties(lvItem, selectedConfig);
					}
				}
			}
		}
		
		private void lvProjects_DoubleClick(object sender, EventArgs e)
		{
			btnEdit_Click(sender, e);
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			if (selectedConfig != null &&
				DialogResult.Yes == Common.GetUserInput("Delete project configuration?", MessageBoxButtons.YesNo))
			{
				cfg.ProjectConfigs.Remove(selectedConfig);
				selectedConfig = null;
				PopulateList();
			}
		}

		internal DialogResult ShowEditForm(ProjectConfig projectConfig)
		{
			DialogResult dlgRes = DialogResult.Cancel;
			using (var frm = new ProjectConfigForm())
			{
				frm.ProjectConfig = projectConfig;
				dlgRes = frm.ShowDialog();
			}
			return dlgRes;
		}

		private void lnkAbout_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			using (var frm = new AboutForm())
			{
				frm.ShowDialog();
			}
		}
	}
}
